home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / AECUR100.ARJ / SPOOLER.C < prev    next >
C/C++ Source or Header  |  1990-03-08  |  4KB  |  191 lines

  1. /*----------------------------------------------------------------------
  2.  *
  3.  *  spooler.c
  4.  *
  5.  *  copyright (c) 1987,88,89,90 J. Alan Eldridge
  6.  *
  7.  *  this is a really stupid print spooler -- but it sort of works
  8.  *
  9.  *  BUGS:
  10.  *
  11.  *  it can use up to MAX file descriptors, since it opens the
  12.  *  file on the call to queue it
  13.  *
  14.  *  NOTES:
  15.  *
  16.  *  you can set the size of the i/o buffer in the initspooler() call;
  17.  *  a size of 0 means to use the default size (see next note)
  18.  *
  19.  *  you can change the default size of the per slice i/o buffer by defining
  20.  *  SP_BUFSIZE like "cc +l -dSP_BUFSIZE=16 -o spooler.o spooler.c" when
  21.  *  you compile it; you can also change the size of the i/o buffer
  22.  *  used to flush the queue; these are things you can best figure out
  23.  *  by trial and error for each application, i'm afraid
  24.  *
  25.  *  same goes for SP_MAXFILES, but i wouldn't get too crazy 'cause i'm not
  26.  *  giving you any more files than dos gives you (this is the main
  27.  *  reason that function spool() has a return code -- no more files)
  28.  *
  29.  *----------------------------------------------------------------------
  30.  */
  31.  
  32. #include "curses.h"
  33. #include "fcntl.h"
  34.  
  35. #define SP_MAXFILES 8
  36. #define SP_BUFSIZE 2
  37. #define SP_MAXBUF 1024
  38.  
  39. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  40.  
  41. /*----------------------------------------------------------------------
  42.  * 
  43.  *  private storage for spooler
  44.  *
  45.  *----------------------------------------------------------------------
  46.  */
  47.  
  48. static int  spcnt=0, 
  49.             spfiles[SP_MAXFILES], 
  50.             spbufsiz=SP_BUFSIZE, 
  51.             taskid = -1;
  52.  
  53. /*----------------------------------------------------------------------
  54.  * 
  55.  *  spooler status functions
  56.  *
  57.  *----------------------------------------------------------------------
  58.  */
  59.  
  60. int
  61. setspoolpri(pri)
  62. int pri;
  63. {
  64.     return setbgpri(taskid, pri);
  65. }
  66.  
  67. int
  68. getspoolpri()
  69. {
  70.     return getbgpri(taskid);
  71. }
  72.  
  73. int
  74. spoolqsize()
  75. {
  76.     return spcnt;
  77. }
  78.  
  79. int
  80. setspoolbuf(bufsize)
  81. int bufsize;
  82. {
  83.     spbufsiz = bufsize ? MIN(bufsize,SP_MAXBUF) : SP_BUFSIZE;
  84. }
  85.  
  86. /*----------------------------------------------------------------------
  87.  * 
  88.  *  spool -- spool a file to the printer
  89.  *
  90.  *----------------------------------------------------------------------
  91.  */
  92.  
  93. int
  94. spool(fn)
  95. char *fn;
  96. {
  97.     if (spcnt < SP_MAXFILES && taskid >= 0) {
  98.         int fd;
  99.  
  100.         fd = open(fn, O_RDONLY);
  101.         if (fd < 0)
  102.             return -1;
  103.         spfiles[spcnt] = fd;
  104.         return spcnt++;
  105.     }
  106.  
  107.     return -1;
  108. }
  109.  
  110. /*----------------------------------------------------------------------
  111.  * 
  112.  *  the spooler task function
  113.  *
  114.  *----------------------------------------------------------------------
  115.  */
  116.  
  117. static void
  118. spooler()
  119. {
  120.     int cnt;
  121.     char buf[SP_MAXBUF];
  122.  
  123.     if (spcnt <= 0)
  124.         return;
  125.     cnt = read(spfiles[0], buf, spbufsiz);
  126.     if (cnt <= 0) {
  127.         int n;
  128.         char ff = 12;
  129.  
  130.         write(4, &ff, 1);
  131.         close(spfiles[0]);
  132.         for (n=0;n<spcnt-1;n++)
  133.             spfiles[n]=spfiles[n+1];
  134.         spcnt--;
  135.     } else
  136.         write(4, buf, cnt);
  137. }
  138.  
  139. /*----------------------------------------------------------------------
  140.  * 
  141.  *  initialization and shutdown functions
  142.  *
  143.  *----------------------------------------------------------------------
  144.  */
  145.  
  146. int
  147. initspooler(pri, bufsiz)
  148. int pri, bufsiz;
  149. {
  150.     if (taskid < 0) {
  151.         spbufsiz = bufsiz ? MIN(bufsiz,SP_MAXBUF) : SP_BUFSIZE;
  152.         taskid = addbgtask(spooler, pri);
  153.     }
  154.  
  155.     return taskid;
  156. }
  157.  
  158. void
  159. spoolabort()
  160. {
  161.     int fdnum, ff = 12;
  162.  
  163.     if (taskid < 0 || !spcnt)
  164.         return;
  165.  
  166.     write(4, &ff, 1);
  167.     for (fdnum=0;fdnum<spcnt;fdnum++)
  168.         close(spfiles[fdnum]);
  169.     spcnt=0;
  170. }
  171.  
  172. void
  173. spoolflush()
  174. {
  175.     int fdnum;
  176.  
  177.     if (taskid < 0)
  178.         return;
  179.  
  180.     for (fdnum = 0; fdnum < spcnt; fdnum++) {
  181.         int cnt, ff = 12;
  182.         char buf[SP_MAXBUF];
  183.  
  184.         while ((cnt = read(spfiles[fdnum], buf, SP_MAXBUF)) > 0)
  185.             write(4, buf, cnt);
  186.         close(spfiles[fdnum]);
  187.         write(4, &ff, 1);
  188.     }
  189.     spcnt = 0;
  190. }
  191.